﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using MySql.Data.MySqlClient;

namespace MovieSystem.Banco
{
    public class EntDesconto
    {
        private int intDescontoId = 0;
       
        /// <summary>
        /// Id
        /// </summary>
        public int Id
        {

            get { return intDescontoId; }
            set { intDescontoId = value; }

        }

        private string strDescontoDescricao = "";
        public string Descricao
        {

            get { return strDescontoDescricao; }
            set { strDescontoDescricao = value; }

        }

        private int intDescontoPercentual = 0;
        public int Percetual
        {

            get { return intDescontoPercentual; }
            set { intDescontoPercentual = value; }

        }

        public Boolean Inserir()
        {

            int intNumero = 0;
            if (EntConexao.AbrirConexao())
            {
                MySqlCommand commS = new MySqlCommand("Insert into desconto (descricaoDesconto, percentualDesconto) values ('" + this.Descricao + "', " + this.Percetual + ");", EntConexao.bdConn);
                intNumero = commS.ExecuteNonQuery();
                
                EntConexao.FecharConexao();
                
            }

            if (intNumero > 0) 
            {
                return true;
            }
            return false;

        }

        public Boolean Alterar()
        {

            int intNumero = 0;
            if (EntConexao.AbrirConexao())
            {
                MySqlCommand commS = new MySqlCommand("update desconto set descricaoDesconto= '" + this.Descricao + "', percentualDesconto = " + this.Percetual + " where idDesconto = " + this.Id + ";", EntConexao.bdConn);
                intNumero = commS.ExecuteNonQuery();

                EntConexao.FecharConexao();

            }
            if (intNumero > 0)
            {
                return true;
            }
            return false;

        }

        public Boolean Excluir()
        {

            int intNumero = 0;
            if (EntConexao.AbrirConexao())
            {
                MySqlCommand commS = new MySqlCommand("delete from desconto where idDesconto = " + this.Id + ";", EntConexao.bdConn);
                intNumero = commS.ExecuteNonQuery();

                EntConexao.FecharConexao();

            }


            return true;

        }

        public List<EntDesconto> BuscaPorParametro(string parametro)
        {
            List<EntDesconto> lDescontos = new List<EntDesconto>();

            if (EntConexao.AbrirConexao())
            {
                MySqlCommand commS = new MySqlCommand("SELECT * FROM desconto where descricaoDesconto like '%" + parametro + "%'", EntConexao.bdConn);
                MySqlDataReader reader = commS.ExecuteReader();

                while (reader.Read())
                {
                    EntDesconto oDesconto = new EntDesconto();
                    oDesconto.Id = Convert.ToInt32(reader["idDesconto"].ToString());
                    oDesconto.Descricao = reader["descricaoDesconto"].ToString();
                    oDesconto.Percetual = Convert.ToInt32(reader["percentualDesconto"].ToString());
                    lDescontos.Add(oDesconto);

                }

                EntConexao.FecharConexao();
            }
            return lDescontos;
        }

        public EntDesconto BuscaPorId(string parametro)
        {
            EntDesconto oDesconto = new EntDesconto();

            if (EntConexao.AbrirConexao())
            {
                MySqlCommand commS = new MySqlCommand("SELECT * FROM desconto where idDesconto =" + parametro + "", EntConexao.bdConn);
                MySqlDataReader reader = commS.ExecuteReader();

                while (reader.Read())
                {
                    oDesconto = new EntDesconto();
                    oDesconto.Id = Convert.ToInt32(reader["idDesconto"].ToString());
                    oDesconto.Descricao = reader["descricaoDesconto"].ToString();
                    oDesconto.Percetual = Convert.ToInt32(reader["percentualDesconto"].ToString());

                }

                EntConexao.FecharConexao();
            }
            return oDesconto;
        }

        public List<EntDesconto> BuscaTodas()
        {
            List<EntDesconto> lDescontos = new List<EntDesconto>();

            if (EntConexao.AbrirConexao())
            {
                MySqlCommand commS = new MySqlCommand("SELECT * FROM desconto;", EntConexao.bdConn);
                MySqlDataReader reader = commS.ExecuteReader();

                while (reader.Read())
                {
                    EntDesconto oDesconto = new EntDesconto();
                    oDesconto.Id = Convert.ToInt32(reader["idDesconto"].ToString());
                    oDesconto.Descricao = reader["descricaoDesconto"].ToString();
                    oDesconto.Percetual = Convert.ToInt32(reader["percentualDesconto"].ToString());
                    lDescontos.Add(oDesconto);


                }

                EntConexao.FecharConexao();
            }
            return lDescontos;
        }
    }
}
